﻿using Dapper;
using MediatR;
using System.Text;
using Warehouse.Api.Read.AGCation.Command;
using Warehouse.Domian.Model;
using Warehouse.ErrorCode;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Api.Read.AGCation.Hanlder
{
    public class GetInventoryTypeCommandHanlder : IRequestHandler<GetInventoryTypeCommand, ApiResult<List<WarehouseTypeModel>>>
    {
        DbContext db;
        ILogger<GetInventoryTypeCommandHanlder> logger;

        public GetInventoryTypeCommandHanlder(DbContext db, ILogger<GetInventoryTypeCommandHanlder> logger)
        {
            this.db = db;
            this.logger = logger;
        }

        public async Task<ApiResult<List<WarehouseTypeModel>>> Handle(GetInventoryTypeCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<WarehouseTypeModel>> result = new ApiResult<List<WarehouseTypeModel>>();
            try
            {
                StringBuilder sqlbuilder = new StringBuilder();
                
                    sqlbuilder.Append(@" SELECT 
                                        TypeId,
                                        TypeName
                                        FROM warehousetypemodel WHERE 0=0");
                    var sql = await db.DapperConnection.QueryAsync<WarehouseTypeModel>(sqlbuilder.ToString());
                    result.Code = ApiEume.Success;
                    result.Msg = "获取库存类型成功";
                    result.Data = sql.ToList();
            }
            catch (Exception ex)
            {

            }
            return result;
        }
    }
}
